//
// Created by v on 19-6-27.
//

#include "CMMqttBase.h"
#include <stdlib.h>
#include <string.h>
#include <strings.h>

CMMqttBase::CMMqttBase(const char *host, int port)
{
    m_host         = host;
    m_port         = port;
    m_sessionClean = true;
    m_function     = NULL;
    m_connected    = false;
    m_debug        = false;
    m_withTls      = false;
    m_insecureTls  = false;
    const char *p  = m_host.c_str();
    const char *pp = index(p, '/');
    if (pp)
    {
        const char *q = rindex(pp + 1, ':');
        if (q)
        {
            char portstr[16] = {0};
            strncpy(portstr, q + 1, 5);
            long int port   = strtol(portstr, NULL, 10);
            m_port          = port;
            size_t len      = q - pp - 2;
            char   url[256] = {0};
            strncpy(url, pp + 2, len >= 256 ? 255 : len);
            url[255] = '\0';
            m_host   = url;
        }
    }
}

CMMqttBase::~CMMqttBase() {}
